/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;

import net.sourceforge.perforce.core.PerforceCorePlugin;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;


/**
  * ClientUser object for the 'p4 print' command.
  * 
  * @version 	$Revision: 33 $
  * @author		<a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Print extends AbstractPerforceCommand 
{
	// --------------------------------------------------------------------------- Private Variables
	
	/** Stores the output of the print operation. */
	private ByteArrayOutputStream result = new ByteArrayOutputStream();

	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Creates a new Print instance.
	  *
	  * @return the new Print instance. 
	  */
	public static final Print newInstance()
	{
		MultiStatus status = 
			new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
			
		return new Print(status);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
	
	/** Constructor for Print.
	  *
	  * @param status the Status object.
	  */
	private Print(MultiStatus status)
	{
		super(status);
	}
	
	// -------------------------------------------------------- Public Methods (IClientUserDelegate)
	
	/** @see AbstractPerforceCommand#outputText(char) */
	public void outputText(byte[] data) 
	{
		try
		{
			result.write(data);
		} 
		catch (IOException e) 
		{
			// May not happen
		}
	}
	
	/** @see AbstractPerforceCommand#outputBinary(char) */
	public void outputBinary(byte[] data) 
	{
		try 
		{
			result.write(data);
		} 
		catch (IOException e) 
		{
			// May not happen
		}
	}
	
	/** @see AbstractPerforceCommand#outputStat(Map) */
	public void outputStat(Map strDict) 
	{
		// Seems as if Perforce 2002.1 always delivers fstat info with print, so we have to
		// override outputStat to avoid UnsupportedOperationExceptions.
	}
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)

	/** @see IPerforceCommand#getCommand() */
	public String getCommand() 
	{
		return "print";                                                               //$NON-NLS-1$
	}
	
	/** @see AbstractPerforceCommand#getStatus() */
	public IStatus getStatus() 
	{
		return getMultiStatus();
	}

	/** @see AbstractPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole() 
	{
		return false;
	}
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Call this method to retrieve the result of the print operation.
	  * 
	  * @return the result as InputStream.
	  */
	public InputStream getResult()
	{
		return new ByteArrayInputStream(result.toByteArray());
	}
}
